package com.yu.boot.admin.web;

import com.lhy.commonweb.model.ResponseResult;
import com.lhy.commonweb.model.ResponseValidate;
import com.lhy.commonweb.service.AbstractService;
import com.lhy.commonweb.web.BaseControllerImpl;
import com.yu.boot.admin.model.CategoryCover;
import com.yu.boot.admin.service.CategoryService;
import com.yu.boot.lib.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 内容管理
 */
@RestController
@RequestMapping("categoryCover")
public class CategoryCoverController extends BaseControllerImpl<CategoryCover, Integer> {


    @Autowired
    CategoryService categoryService;

    public CategoryCoverController(@Qualifier("categoryCoverService") AbstractService service) {
        super(service);
    }


    @Override
    public ResponseResult<CategoryCover> create(@RequestBody CategoryCover bean) {
        super.create(bean.setCreated(new Date()));
        return new ResponseResult<CategoryCover>().setSuccess(true).setData(bean);
    }


    @Override
    public ResponseResult<CategoryCover> delete(@PathVariable("id") Integer id) {
        categoryService.deleteByCover(String.valueOf(id));
        return super.delete(id);
    }

    @Override
    public ResponseValidate validate(CategoryCover bean) {
        if (bean.getId() != null) {
            CategoryCover categoryCover = service.get(bean.getId());
            if (categoryCover == null) {
                bean.setId(null);
                return super.validate(bean);
            } else {
                if (categoryCover.getCoverName().equals(bean.getCoverName())) {
                    return ResponseValidate.ok();
                } else {
                    bean.setId(null);
                    return super.validate(bean);
                }
            }
        }
        return super.validate(bean);
    }
}
